package com.example.yiwei.crm.view.activity.customerActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.data.GetContact;
import com.example.yiwei.crm.data.GetCustumer;
import com.example.yiwei.crm.entity.BZFLEntity;
import com.example.yiwei.crm.entity.ContactMessageEntity;
import com.example.yiwei.crm.entity.CustomerMessageEntity;
import com.example.yiwei.crm.json.YYSpinnerJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.activity.coordinationActivity.CoorCustomerActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class CustomerAddMessageActivity extends AppCompatActivity {
    private EditText edit_js,edit_bx,edit_nextcont,edit_bz;
    private Spinner spinner_yl,spinner_rh,spinner_hy;
    private SharedPreferences sp;
    List<BZFLEntity> list;
    List<String> alist;
    private ArrayAdapter adapter;
    private String ylname;
    private int ylid,fkid,hyid;
    private CustomerMessageEntity customer;
    private ContactMessageEntity contact;
    String sresult;
    int customer_id;
    ImageView im_comment;
    List<String> fk_list,hy_list;
    private ArrayAdapter adapter5,adapter6;
    private String fk_name,hy_name;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_add_message);
        edit_js = (EditText) findViewById(R.id.userAdd_edit_introducer);
        edit_bx = (EditText) findViewById(R.id.userAdd_edit_security_numbers);
        edit_nextcont = (EditText) findViewById(R.id.userAdd_edit_content);
        edit_bz = (EditText) findViewById(R.id.userAdd_edit_additional_remarks);
        spinner_yl = (Spinner) findViewById(R.id.userAdd_spinner_medical_class);
        spinner_rh = (Spinner) findViewById(R.id.add_customer_rufk);
        spinner_hy = (Spinner) findViewById(R.id.add_customer_hydj);
        im_comment = (ImageView) findViewById(R.id.userAdd_img_commit);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);

        new Thread(new Runnable() {
            @Override
            public void run() {
                SC sc = new SC();
                JSONObject object = sc.storage(sp);
                String json = object.toString();
                String result = NetWorkUtils.getCount(baseClass.find_YLLB_URL,json);
                list = YYSpinnerJson.getYLLB(result);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        alist = new ArrayList<String>();
                        for (int i =0;i<list.size();i++){
                            alist.add(list.get(i).getName());
                        }
                        adapter = new ArrayAdapter(CustomerAddMessageActivity.this,R.layout.spinner_display_style, R.id.txtvwSpinner, alist);
                        adapter.setDropDownViewResource(R.layout.spinner_dropdown_style);
                        spinner_yl.setAdapter(adapter);
                        spinner_yl.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                ylname = list.get(position).getName();
                                ylid = list.get(position).getId();
                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {

                            }
                        });
                        if ("updata".equals(getIntent().getStringExtra("updata"))){
                            setMessage();
                        }
                    }
                });
            }
        }).start();
        fk_list=new ArrayList<>();
        hy_list=new ArrayList<>();
        fk_list.add(0,"");
        fk_list.add(1,"未表态");
        fk_list.add(2,"糊涂");
        fk_list.add(3,"语言沟通障碍");
        fk_list.add(4,"未见面");
        fk_list.add(5,"无主见活动能力");
        fk_list.add(6,"拒绝沟通");
        hy_list.add(0,"");
        hy_list.add(1,"A");
        hy_list.add(2,"B");
        hy_list.add(3,"C");
        hy_list.add(4,"VIP");
        adapter5 = new ArrayAdapter(CustomerAddMessageActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, fk_list);
        adapter5.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spinner_rh.setAdapter(adapter5);
        adapter6 = new ArrayAdapter(CustomerAddMessageActivity.this, R.layout.spinner_display_style, R.id.txtvwSpinner, hy_list);
        adapter6.setDropDownViewResource(R.layout.spinner_dropdown_style);
        spinner_hy.setAdapter(adapter6);
        spinner_rh.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                fkid = position;
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        spinner_hy.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                hyid = position;
            }
            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.userAdd_img_commit:
                im_comment.setVisibility(View.GONE);
                add();
                break;
            case R.id.customer_add_message_btn_back:
                finish();
                break;
        }
    }
    public void add(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                customer = GetCustumer.get();
                contact = GetContact.get();
                SC sc = new SC();
                JSONObject json=sc.storage(sp);
                try {
                    json.put("KAA02",customer.getClient_name());
                    json.put("KAA03", customer.getClient_age());
                    json.put("KAA04", customer.getClient_gender());
                    json.put("KAA06",customer.getIDNumber());
                    json.put("KAA08",customer.getClient_Address());
                    if (customer.getClient_Manager()!=0){
                        json.put("KAA09",customer.getClient_Manager());
                        json.put("KAA09A",customer.getClient_manager_name());
                    }
                    if ("updata".equals(getIntent().getStringExtra("updata"))){
                        json.put("addoredit","edit");
                        json.put("KAA01",customer.getClient_id());
                    }else {
                        json.put("addoredit","add");
                    }
                    json.put("KXA01",getIntent().getIntExtra("dockid",0));
//                    if ("updata".equals(getIntent().getStringExtra("updata"))){
//                        json.put("addoredit","edit");
//                    }else {
//                        json.put("addoredit","add");
//                    }

                    json.put("KAA23",customer.getPid());
                    json.put("KAA24",customer.getCid());
                    json.put("KAA25",customer.getAid());
                    json.put("KAA30",customer.getPname());
                    json.put("KAA31",customer.getCname());
                    json.put("KAA32",customer.getAname());
                    json.put("KAA05",customer.getBirthDay());
                    json.put("KAA10",customer.getNext_Time());
                    json.put("KLA06",contact.getFirst_contact_mobile_phone());
                    json.put("CID03",sp.getString("CID03",null));
                    json.put("KLA02",contact.getContact_name());
                    json.put("KLA14",1);
                    json.put("KLA07",contact.getSend_contact_phone());
                    json.put("KLA04",contact.getContact_respectful());
                    json.put("KLA10",contact.getContact_qq());
                    json.put("KLA11",contact.getContact_weibo());
                    json.put("KLA12",contact.getContact_wechat());
                    json.put("KLA13",contact.getContact_address());
                    json.put("KAA21",edit_js.getText().toString());
                    json.put("AKE01",ylid);
                    json.put("KAA12",edit_bx.getText().toString());
                    json.put("KAA34",fkid);
                    json.put("KAA37",hyid);
                    json.put("KAA11",edit_nextcont.getText().toString());
                    json.put("REK01",edit_bz.getText().toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                String sjson=json.toString();

                    //新增客户信息
                    sresult = NetWorkUtils.getCount(baseClass.add_coor_customer_URL, sjson);


                Log.d("cc",sresult);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (sresult!=null){
                            try {
                                JSONObject object = new JSONObject(sresult);
                                boolean b = object.getBoolean("ifsuccess");
                                if (b){
                                    if ("updata".equals(getIntent().getStringExtra("updata"))){
                                        CustomerAddActivity.a.finish();
                                        CustomerAddContactMessageActivity.a.finish();
                                        finish();
                                    }else {
                                        customer_id = object.getInt("KAA01");
                                        AlertDialog.Builder builder1;
                                        builder1 = new AlertDialog.Builder(new ContextThemeWrapper(CustomerAddMessageActivity.this, R.style.custom_dialog));
                                        builder1.setTitle("是否跳转预约页面？");
                                        builder1.setPositiveButton("是", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Intent intent = new Intent(CustomerAddMessageActivity.this,CustomerAppointmentAcitity.class);
                                                intent.putExtra("dockid",getIntent().getIntExtra("dockid", 0));
                                                intent.putExtra("ID",customer_id);
                                                startActivity(intent);
                                                CustomerAddActivity.a.finish();
                                                CustomerAddContactMessageActivity.a.finish();
                                                finish();
                                            }
                                        });
                                        builder1.setNegativeButton("否", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                CustomerAddActivity.a.finish();
                                                CustomerAddContactMessageActivity.a.finish();
                                                Intent intent = new Intent(CustomerAddMessageActivity.this, CoorCustomerActivity.class);
                                                intent.putExtra("dockid",getIntent().getIntExtra("dockid",0));
                                                startActivity(intent);
                                                finish();
                                            }
                                        });
                                        builder1.setCancelable(false);
                                        builder1.show();
                                    }
                                }else {
                                    im_comment.setVisibility(View.VISIBLE);
                                    Toast.makeText(CustomerAddMessageActivity.this, "添加客户失败", Toast.LENGTH_SHORT).show();
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                });
            }
        }).start();
    }
    public void setMessage(){
        customer = GetCustumer.get();
        for (int i= 0;i<list.size();i++){
            if (list.get(i).getId()==customer.getMedical_class_id()){
                spinner_yl.setSelection(i);
            }
        }
        spinner_hy.setSelection(customer.getHydj()
        );
        spinner_rh.setSelection(customer.getRhfk());
        edit_js.setText(customer.getIntroducer());
        edit_nextcont.setText(customer.getNext_Content());
        edit_bz.setText(customer.getAdditional_remarks());
        edit_bx.setText(customer.getSecurity_numbers());
    }
}
